一个专业的 代码仓库历史 是一段精心编排的叙事,而非偶然的日记。通过优先采用 原子快照 和有策略的变基操作,开发者将时间线转化为可搜索、易读的文档。
1. 提交即有意的快照
不要将 git commit 当作“保存”按钮,而应将每次提交视为一个逻辑里程碑。经验法则:"为项目中的每一项重要变更提交一次快照,"以及"如果无法用一句具体的话描述变更内容,就不要提交。"
2. 线性理想的实现
变基 通过将一个分支移动到另一个分支的顶端,实现快速前进式合并。这实际上消除了对合并提交的需求,从而形成完全 线性的历史。
3. 规律与精炼
像 git commit --amend 这类工具允许你将暂存的更改添加到最近的一次提交中。这能在推送到共享远程仓库前修复错误,保持叙事的完整性。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which command is used to add staged changes to the most recent commit instead of creating a new one?
git merge --no-ff
git commit --amend
git rebase --continue
git reset --soft
✅ Correct!
Correct! --amend lets you refine your latest local commit.❌ Incorrect
Check the quick reference for modifying the most recent snapshot.QUESTION 2
What is the primary benefit of rebasing according to the 'Linear Ideal'?
It preserves every single 'work-in-progress' commit.
It effectively eliminates the need for merge commits.
It prevents the use of git log.
It automatically pushes changes to the remote.
✅ Correct!
Yes! Rebasing allows for fast-forward merges, keeping the history vertical and readable.❌ Incorrect
Rebasing focuses on moving a branch to the tip of another to avoid 'tangled' merge bubbles.QUESTION 3
True or False: You should commit a snapshot even if you cannot describe it with a single specific message.
True
False
✅ Correct!
False. If you can't name the change specifically, it likely isn't an atomic or significant snapshot.❌ Incorrect
The philosophy suggests waiting until you have a specific, logical addition to commit.QUESTION 4
What does
git merge --no-ff accomplish?It deletes the branch after merging.
It forces a merge commit even if a fast-forward is possible.
It performs a rebase instead of a merge.
It ignores all local changes.
✅ Correct!
Correct. This is used to intentionally create a visible milestone in the repository history.❌ Incorrect
The --no-ff flag prevents Git from simply moving the pointer forward without a merge node.QUESTION 5
Which command provides a condensed, one-line view of the commit history?
git status --short
git log --oneline
git show --summary
git reflog
✅ Correct!
Yes! This is the standard tool for auditing a clean, linear history.❌ Incorrect
Try the log command variation that keeps things to 'one line' per entry.Case Study: The Feature Cleanup
Applying the Narrative Philosophy
A developer has five local commits: 'fix typo', 'tmp', 'wip', 'actual fix', and 'forgot one file'. They want to merge this into the master branch as a single professional entry labeled 'Implement user authentication'.
Q
1. Which tool should the developer use to combine these five messy commits into one before merging?
Solution:
The developer should use an interactive rebase (
The developer should use an interactive rebase (
git rebase -i) to 'squash' the work-in-progress commits into a single, clean snapshot.Q
2. If the developer wants to ensure the 'master' branch remains linear without 'bubbles' from this feature, what merge strategy should they use?
Solution:
They should use a fast-forward merge (default) or rebase the feature branch onto the tip of master first. This moves the branch pointer to the tip without a merge commit.
They should use a fast-forward merge (default) or rebase the feature branch onto the tip of master first. This moves the branch pointer to the tip without a merge commit.